import pygame
from pygame import sprite

import baseItem
from explode import Explode
from music import Music

images_path = "./images/"
bullet_u = images_path + "MU.gif"
bullet_D = images_path + "MD.gif"
bullet_L = images_path + "ML.gif"
bullet_r = images_path + "MR.gif"


class Bullet(baseItem.BaseItem):
    def __init__(self, tank):
        super().__init__()
        self.tank = tank
        self.direction = tank.direction
        self.images = {
            "U": pygame.image.load(bullet_u),
            "D": pygame.image.load(bullet_D),
            "L": pygame.image.load(bullet_L),
            "R": pygame.image.load(bullet_r),
        }
        self.image = self.images[self.direction]
        self.rect = self.image.get_rect()
        self.speed = 5
        self.live = True
        if self.direction == "U":
            self.rect.left = tank.rect.left + tank.rect.width / 2 - self.rect.width / 2
            self.rect.top = tank.rect.top
        elif self.direction == "D":
            self.rect.left = tank.rect.left + tank.rect.width / 2 - self.rect.width / 2
            self.rect.top = tank.rect.top + tank.rect.height - self.rect.height
        elif self.direction == "L":
            self.rect.left = tank.rect.left
            self.rect.top = tank.rect.top + tank.rect.height / 2 - self.rect.height / 2
        elif self.direction == "R":
            self.rect.left = tank.rect.left + tank.rect.width - self.rect.width / 2
            self.rect.top = tank.rect.top + tank.rect.height / 2 - self.rect.height

    def move(self, screen_width, screen_height):
        if self.direction == "U":
            if self.rect.top > 0:
                self.rect.top -= self.speed
            else:
                # 撞墙
                self.live = False
        elif self.direction == "D":
            if self.rect.top < screen_height - self.rect.height:
                self.rect.top += self.speed
            else:
                # 撞墙
                self.live = False
        elif self.direction == "L":
            if self.rect.left > 0:
                self.rect.left -= self.speed
            else:
                # 撞墙
                self.live = False
        elif self.direction == "R":
            if self.rect.left < screen_width - self.rect.width:
                self.rect.left += self.speed
            else:
                # 撞墙
                self.live = False

    def display(self, window):
        # 展示子弹
        window.blit(self.image, self.rect)

    def hit_ey_tank(self, ey_tank_list,explode_list):
        for ey_tank in ey_tank_list:
            if sprite.collide_rect(self, ey_tank):
                self.live = False
                ey_tank.live = False
                # 爆炸效果
                explode_list.append(Explode(ey_tank))
                music = Music("images/explode.mp3")
                music.play()

    def hit_my_tank(self, my_tank,explode_list):
        if sprite.collide_rect(self, my_tank):
            self.live = False
            my_tank.live = False
            # 爆炸效果
            explode_list.append(Explode(my_tank))
            music = Music("images/explode.mp3")
            music.play()

